
OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
    defaultHandlerOptions: {
        'single': true,
        'double': false,
        'pixelTolerance': 0,
        'stopSingle': false,
        'stopDouble': false
    },
    
    initialize: function(options){
        this.handlerOptions = OpenLayers.Util.extend({}, this.defaultHandlerOptions);
        OpenLayers.Control.prototype.initialize.apply(this, arguments);
        this.handler = new OpenLayers.Handler.Click(this, {
            'click': this.trigger
        }, this.handlerOptions);
    },
    trigger: function(e){
        var vectorLayer = this.vectorLayer;
        var lonlat = this.map.getLonLatFromViewPortPx(e.xy);
        var projection = new OpenLayers.Projection('EPSG:900913');
        var displayProjection = new OpenLayers.Projection('EPSG:4326');
		var fn = this.onComplete;
        xy = OpenLayers.Projection.transform(new OpenLayers.Geometry.Point(lonlat.lon, lonlat.lat), projection, displayProjection);
        var gh = encodeGeoHash(xy.y, xy.x);
        gh = gh.substr(0, 8);//using 20m as accuracy
        boxes = {
            center: gh
        };
        position = ['top', 'bottom', 'left', 'right'];
		var i;
        for (i = 0; i < 4; i++) {
            boxes[position[i]] = calculateAdjacent(gh, position[i]);
            
        }
        boxes['topleft'] = calculateAdjacent(boxes['left'], 'top');
        boxes['topright'] = calculateAdjacent(boxes['right'], 'top');
        boxes['bottomright'] = calculateAdjacent(boxes['right'], 'bottom');
        boxes['bottomleft'] = calculateAdjacent(boxes['left'], 'bottom');
        //console.log(boxes);
        var neighbors = []
        var p;
		
        for (p in boxes) {
//			alert(boxes[p])
            neighbors.push(boxes[p]);
        };
        
        url = '/vector/ZGF0YS92ZWN0b3Iuc3FsaXRl/' + neighbors.join(',');
        
        function onComplete(r){
            //alert(r.responseText);
            deserialize(r.responseText, e, vectorLayer);
        }
        OpenLayers.loadURL(url, {}, null, onComplete, null)

        
        
        
    }
    
});



//load geojson
function deserialize(geojson, e, vectors){
    var in_options = {
        'internalProjection': new OpenLayers.Projection('EPSG:900913'),
        'externalProjection': new OpenLayers.Projection('EPSG:4326')
    };
    var format = new OpenLayers.Format.GeoJSON(in_options);
    var features = format.read(geojson);
    var bounds;
    if (features) {
        vectors.removeAllFeatures();
        if (features.constructor != Array) {
            features = [features];
        }
		var i;
        for (i = 0; i < features.length; ++i) {
            if (!bounds) {
                bounds = features[i].geometry.getBounds();
            }
            else {
                bounds.extend(features[i].geometry.getBounds());
            }
            
        }
        vectors.addFeatures(features);
        //map.zoomToExtent(bounds);
    
    }
};
